<template>
  <div class="app-container" style="background: #FFFFFF;margin:10px;height: calc(100vh - 70px);">
 
 <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="经销商" prop="deaName">
        <el-input
          v-model="queryParams.deaName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['DealerMain:dealer:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['DealerMain:dealer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['DealerMain:dealer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['DealerMain:dealer:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="tbDeal"
        v-if="tbshow"
          >同步经销商信息</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dealerList"
      @selection-change="handleSelectionChange" height="450px"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="经销商id" align="center" prop="deaId" v-if="false"/>
      <el-table-column label="经销商名称/帐号" align="left" prop="deaName">
        <template slot-scope="scope" >
          <span style="white-space: pre-wrap;">
            {{scope.row.deaName+"\n"}}
            {{scope.row.deaUser}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="帐户余额(¥)" align="center" prop="a1" />
      <el-table-column label="手机号码" align="center" prop="deaTel" />
      <el-table-column label="电子邮箱" align="center" prop="deaEmail" />
      <el-table-column label="业务开展地区" align="center" prop="" />
      <el-table-column label="客户经理" align="center" prop="deaPeople" />
  
      <el-table-column label="经销商类型" align="center" prop="deaType">
        <template slot-scope="scope">
          <span style="white-space: pre-wrap;">
            {{scope.row.deaType=='1' ?'一级经销商':'二级经销商'}}
          </span>
        </template>
      </el-table-column>
            <el-table-column
        label="关联时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> 
      <el-table-column label="状态" align="left" prop="other1">
        <template slot-scope="scope">
          <span slot="reference" v-if="scope.row.other1 == 3">
              <div class="dotClass" style="background-color: blue;">   </div>
              <div style="float: left; margin-left: 10px;">已认证</div>
            </span>
            <span slot="reference" v-if="scope.row.other1 == 1">
              <div class="dotClass" style="background-color: springgreen;">   </div>
              <div style="float: left; margin-left: 10px;">已关联</div>
            </span>
            <span  v-if="scope.row.other1 == null">
              <div class="dotClass" style="background-color: red;">   </div>
              <div style="float: left;  margin-left: 10px;">未关联</div>
            </span>
 
          </template>
      </el-table-column>
        <el-table-column label="启用状态" align="center" key="deaState" >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.deaState"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column> 
       <!-- <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="修改人" align="center" prop="editUser" /> -->


      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['DealerMain:dealer:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['DealerMain:dealer:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改一级经销商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="经销商" prop="deaName">
          <el-input v-model="form.deaName" placeholder="请输入经销商名称" />
        </el-form-item>
        <el-form-item label="经销商ID" prop="deaId" v-if="false">
          <el-input v-model="form.deaId" placeholder="请输入经销商名称" />
        </el-form-item>
        <el-form-item label="经销商类型" prop="dtype" v-if="false">
          <el-input v-model="form.dtype" placeholder="请输入经销商名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录名" prop="deaUser">
              <el-input v-model="form.deaUser" :disabled="deadis" placeholder="请输入登录名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="deaPassword">
              <el-input v-model="form.deaPassword" placeholder="请输入密码" show-password/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="渠道类型" prop="accountLx">
            <el-select
              v-model="queryParams.accountLx"
              placeholder="渠道类型"
              clearable
             
            >
              <el-option
                v-for="dict in dict.type.sys_dea_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="deaTel">
              <el-input v-model="form.deaTel" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="deaEmail">
              <el-input v-model="form.deaEmail" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态">
              <el-radio-group v-model="form.deaState">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="公司地址" prop="deaAddress">
          <el-input
            v-model="form.deaAddress"
            type="textarea"
            placeholder="请输入公司地址"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
 
  </div>
</template>

<script>

import {
  listDealer,
  getDealer,
  delDealer,
  addDealer,
  updateDealer,
  tblistDealer,
} from "@/api/DealerMain/dealer";

export default {
  dicts: ["sys_normal_disable","sys_dea_type"],
  name: "Dealer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一级经销商表格数据
      dealerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deadis:true,
      deatabname:'',//标签名称
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deaName: null,
        deaUser: null,
        deaPassword: null,
        deaPeople: null,
        deaTel: null,
        deaEmail: null,
        deaAddress: null,
        deaType: null,
 
        createUser: null,
        editUser: null,
        editTime: null,
        other1: null,
        other2: null,
        other3: null,
        other4: null,
        other5: null,
        dtype: null,
        accountLx:null,
      },
      tbshow:false, //同步经销商按钮是否显示
 
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deaName: [
          { required: true, message: "经销商名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 50 之间",
            trigger: "blur",
          },
        ],
        deaUser: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
 
        deaPassword: [
          { required: true, message: "用户密码不能为空", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "用户密码长度必须介于 5 和 20 之间",
            trigger: "blur",
          },
        ],
        deaEmail: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        deaTel: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.queryParams.dtype = this.$route.query.a;//获取路由上的类型，一代，二代，华为二代
 
   // alert(this.queryParams.dtype);
    this.getList();
 
  },
  methods: {

     handleClick(tab, event) {
        console.log(tab, event);
      },
    /** 查询一级经销商列表 */
    getList() {
      this.loading = true;
      listDealer(this.queryParams).then((response) => {
        this.dealerList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.dealerList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deaId: null,
        deaName: null,
        deaUser: null,
        deaPassword: null,
        deaPeople: null,
        deaTel: null,
        deaEmail: null,
        deaAddress: null,
        deaType: null,
        deaState: "0",
        createUser: null,
        editUser: null,
        createTime: null,
        editTime: null,
        other1: null,
        other2: null,
        other3: null,
        other4: null,
        other5: null,
        dtype: null,
      };
      this.form.dtype=this.$route.query.a;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deaId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.deadis=false;
      this.open = true;
 
      this.title = "添加渠道信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.deadis=true;
      const deaId = row.deaId || this.ids;
      getDealer(deaId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.deaId != null) {
            updateDealer(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDealer(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deaIds = row.deaId || this.ids;
  
      this.$modal
        .confirm('是否确认删除渠道编号为"' + deaIds + '"的数据项？')
        .then(function () {
          return delDealer(deaIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "DealerMain/dealer/export",
        {
          ...this.queryParams,
        },
        `dealer_${new Date().getTime()}.xlsx`
      );
    },
         /** 同步经销商 */
       tbDeal() {
      this.loading = true;
      tblistDealer(this.queryParams).then((response) => {
        this.getList();
        this.loading = false;
      });
    },
  },
};
</script>
<style>
.dotClass {
  width:10px;
  height:10px;
  float: left;   margin-top: 6px;
  border-radius: 50%;
  display: block;

}
 
</style>